<template>
	<view class="container">
		<!-- 买家信息以及地址 -->
		<view class="buyer-info" @click="chooseAddr">
			<view class="" v-if="address_ok">
				<view class="name-phone">
					<view class="name">{{userName}}</view>
					<view class="phone">{{telNumber}}</view>
				</view>
				<view class="address">{{provinceName}}{{cityName}}{{countyName}}{{detailInfo}}</view>
			</view>
			<view class=""  v-if="!address_ok">请选择您的收货地址</view>
			<view class="change-address">
				<image lazy-load="true"  src="../../static/images/arrow-right.png" class="change-address"></image>
			</view>
		</view>
		<!-- 单个店铺的所有商品列表 -->
		<view class="item-box">
			<view class="item-title">
				<view class="shop-ico"><image lazy-load="true"  src="../../static/images/shop-ico.png" class="shop-ico"></image></view>
				<view class="shop-name">{{goods.store.name}}</view>
			</view>
			<view class="item">
				<view class="item-info">
					<view class="item-img">
						<image lazy-load="true"  :src="goods.thumbnail" mode="aspectFill"  class="item-img"></image>
					</view>
					<view class="item-text">
						<view class="title">{{goods.name}}</view>
						<view class="type">
							<view>规格</view>
							<view class="size">{{goods.spec}}</view>
						</view>
						<view class="price-amount">
							<view class="price">￥{{goods.real_price}}</view>
							<view class="amount">
								<view class="times">×</view>
								<view class="item-num">{{goods_num}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 配送方式及运费 -->
			<view class="delivery-info">
				<view class="delivery">
					<view>配送方式</view>
					<view>物流配送</view>
				</view>
				<view class="line"></view>
				<view class="delivery-cost">
					<view>运费</view>
					<view>￥{{delivery_cost}}</view>
				</view>
			</view>
		</view>
		<view class="plus">
			<textarea class="plus-info" maxlength="100" @blur="bindTextAreaBlur" placeholder="备注（选填）：请填写与商家协商一致的信息" />
		</view>
		<view class="empty"></view>
		<view class="submit-menu">
			<view class="text-box">
				<view class="goods-total"><view>共</view><view>{{goods_num}}</view><view>件商品</view></view>
				<view class="total-price">
					<view class="total">合计：</view>
					<view class="price">￥{{count_money}}</view>
				</view>
			</view>	
			<view class="submit-btn" @click="payMoney">结算</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				"checked":true,
				"checkAllSta":false,
				"store_sku_id":0,
				"goods_num":1,
				"real_price":0,
				"delivery_cost":0,
				"address_ok":false,  //
				"goods":{},
				"count_money":0,
				"userName":'',
				"provinceName":'',
				"cityName":'',
				"countyName":'',
				"detailInfo":'',
				"telNumber":'',
				"share_user_id":0,
				"note_id":0,
				"remark":''
			}
		},
		onLoad(e) {
			var self = this;
			this.store_sku_id = e.id;
			this.goods_num = e.num;
			this.count_money = parseFloat(this.real_price) * parseInt(this.goods_num);
			this.getGoodsData();
			this.share_user_id = uni.getStorageSync('share_id') ? parseInt(uni.getStorageSync('share_id')) : 0 ;
			this.goods_id = uni.getStorageSync('goods_id') ? parseInt(uni.getStorageSync('goods_id')) : 0;
			this.note_id = uni.getStorageSync('note_id') ? parseInt(uni.getStorageSync('note_id')) : 0;
			// #ifdef H5
			let share_time_line={
				title:self.$request.h5_share_name, // 分享标题
				link: self.$request.h5_share_url+'?page=index', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: self.$request.h5_img_url+'/static/images/logo.png', // 分享图标
			}
			let share_app_message={
				title: self.$request.h5_share_name, // 分享标题
				desc: '我发现了一个很不错的商城，买家具有超大实惠！', // 分享描述
				link: self.$request.h5_share_url+'?page=index', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: self.$request.h5_img_url+'/static/images/logo.png', // 分享图标
			}
			uni.setStorageSync('share_time_line',share_time_line);
			uni.setStorageSync('share_app_message',share_app_message);
			self.$request.wx_config()
			// #endif
		},
		onShareAppMessage(res) {
		    if (res.from === 'button') {
		      console.log(res.target)
		    }
		    return {
		      title: this.$request.share_title,
		      path: '/pages/index/index'
		    }
		  },
		methods: {
			payMoney(){
				var self = this;
				if(this.provinceName==''){
					uni.showToast({
					    title: '收货地址不能为空~',
					    icon: 'none',
					    duration: 2000
					});
					return false;
				}
				this.$request.request({
					url:'/order/create',
					data:{
						store_sku_id:this.store_sku_id,
						count:this.goods_num,
						share_user_id:this.share_user_id,
						note_id:this.note_id,
						province:this.provinceName,
						city:this.cityName,
						area:this.countyName,
						address:this.detailInfo,
						consignee:this.userName,
						tel:this.telNumber,
						remark:this.remark
					},
					success:function(res){
						if(res.code==200){
							
							
							// #ifdef MP-WEIXIN
							uni.requestPayment({
								timeStamp:res.result.timeStamp,
								nonceStr:res.result.nonceStr,
								package:res.result.package,
								signType:res.result.signType,
								paySign:res.result.paySign,
								success:function () {
									uni.showToast({
									    title: '支付成功！',
									    duration: 2000
									});
									setTimeout(function() {
										uni.redirectTo({
										    url: '../my-order/my-order?id=2'
										});
									}, 2000);
								},
								fail:function () {
									uni.showToast({
									    title: '支付失败，请稍后重试',
										icon: 'none',
									    duration: 2000
									});
									setTimeout(function() {
										uni.redirectTo({
										    url: '../my-order/my-order?id=1'
										});
									}, 2000);
								}
									
							})
							// #endif
							
							// #ifdef H5
							self.wx.chooseWXPay({
							  timestamp: res.result.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
							  nonceStr: res.result.nonceStr, // 支付签名随机串，不长于 32 位
							  package: res.result.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
							  signType: res.result.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
							  paySign:res.result.paySign, // 支付签名
							  success: function (res) {
							    uni.showToast({
							        title: '支付成功！',
							        duration: 2000
							    });
							    setTimeout(function() {
							    	uni.redirectTo({
							    	    url: '../my-order/my-order?id=2'
							    	});
							    }, 2000);
							  },
							  fail:function () {
							  	uni.showToast({
							  	    title: '支付失败，请稍后重试',
							  		icon: 'none',
							  	    duration: 2000
							  	});
							  	setTimeout(function() {
							  		uni.redirectTo({
							  		    url: '../my-order/my-order?id=1'
							  		});
							  	}, 2000);
							  }
							});
							
							// #endif
							
						}else if(res.code==40003){
							uni.navigateTo({
							    url: '../login/login?page=order&id='+self.store_sku_id +'&num='+self.goods_num
							});
						}else{
							
						}
					}
				})
			},
			bindTextAreaBlur(e){
				this.remark = e.detail.value;
			},
			getGoodsData(){
				var self = this;
				this.$request.request({
					url:'/goods/detail',
					data:{
						store_sku_id:this.store_sku_id
					},
					success:function(res){
						if(res.code==200){
							var x = res.result;
							x.detail = JSON.parse(x.detail);
							x.image = JSON.parse(x.image);
							x.detail.forEach((item,index)=>{
								x.detail[index] =self.$http_img(item);
							});
							x.spec_list.forEach((item,index)=>{
								x.spec_list[index].checked =false;
							});
							x.image.forEach((item,index)=>{
								x.image[index] =self.$http_img(item);
							});
							x.thumbnail =self.$http_img(x.thumbnail);
							x.price =self.$money(x.price);
							x.delivery_cost =self.$money(x.delivery_cost);
							x.real_price =self.$money(x.real_price);
							x.store.logo =self.$http_img(x.store.logo);
							self.goods = x;
							self.real_price=x.real_price;
							self.delivery_cost=parseFloat(x.delivery_cost) * parseInt(self.goods_num);
							self.count_money = parseFloat(self.real_price) * parseInt(self.goods_num) + self.delivery_cost;
						}else{
							
						}
					}
				})
			},
			chooseAddr(){
				var self = this;
				// #ifdef MP-WEIXIN
				uni.chooseAddress({
				  success(res) {
				    console.log(res.userName)
				    console.log(res.provinceName)
				    console.log(res.cityName)
				    console.log(res.countyName)
				    console.log(res.detailInfo)
				    console.log(res.telNumber)
					self.userName = res.userName;
					self.provinceName = res.provinceName;
					self.cityName = res.cityName;
					self.countyName = res.countyName;
					self.detailInfo = res.detailInfo;
					self.telNumber = res.telNumber;
					self.address_ok = true;
				  }
				})
				// #endif
				// #ifdef H5 
				self.wx.openAddress({
				  success: function(res) {
					self.userName = res.userName;
					self.provinceName = res.provinceName;
					self.cityName = res.cityName;
					self.countyName = res.countryName;
					self.detailInfo = res.detailInfo;
					self.telNumber = res.telNumber;
					self.address_ok = true;
				  }
				});
				
				// #endif
			},
		}
	}
</script>

<style>
	@import url("order.css");
</style>
